<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="资源分类" prop="resourceClassifyId">
        <el-select v-model="formData.resourceClassifyId" clearable filterable placeholder="请选择">
          <el-option
            @click="formData.resourceClassifyName = dict.name"
            v-for="dict in classifyList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资源名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入资源名称" />
      </el-form-item>
      <el-form-item label="别名" prop="alias">
        <el-input v-model="formData.alias" placeholder="请输入别名" />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入描述" />
      </el-form-item>
      <el-form-item label="页面截图" prop="picUrl">
        <UploadImg v-model="formData.picUrl" />
      </el-form-item>
      <el-form-item label="视频介绍" prop="videoUrl">
        <UploadFile v-model="formData.videoUrl" />
      </el-form-item>
      <el-form-item label="支持系统" prop="supportSystemList">
        <el-checkbox-group v-model="formData.supportSystemList">
          <el-checkbox
            v-for="dict in getStrDictOptions('cms_support_system')"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="其他下載地址" prop="otherDownloadUrl">
        <el-input v-model="formData.otherDownloadUrl" placeholder="请输入其他下載地址" />
      </el-form-item>
      <el-form-item label="激活码购买地址" prop="activityUrl">
        <el-input v-model="formData.activityUrl" placeholder="请输入激活码购买地址" />
      </el-form-item>
      <el-form-item label="优惠码" prop="activityCode">
        <el-input v-model="formData.activityCode" placeholder="请输入优惠码" />
      </el-form-item>
      <el-form-item label="是否测试" prop="testStatus">
        <el-radio-group v-model="formData.testStatus">
          <el-radio
            v-for="dict in getStrDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="小提示" prop="tips">
        <el-input v-model="formData.tips" placeholder="请输入小提示" />
      </el-form-item>
      <el-form-item label="轮播展示" prop="home">
        <el-radio-group v-model="formData.home">
          <el-radio
            v-for="dict in getStrDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="相关分类" prop="detail">
        <el-input v-model="formData.detail" placeholder="相关分类" />
      </el-form-item>
      <el-form-item label="上架状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="dict in getStrDictOptions('cms_resource_status')"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { ResourceDirApi, ResourceDirVO } from '@/api/system/resourcedir'
import { ResourceClassifyApi } from '@/api/system/resourceclassify'

/** 资源命名 表单 */
defineOptions({ name: 'ResourceDirForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const classifyList = ref<any>([])
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  resourceClassifyName: undefined,
  resourceClassifyId: undefined,
  name: undefined,
  alias: undefined,
  detail: undefined,
  remark: undefined,
  picUrl: undefined,
  videoUrl: undefined,
  supportSystemList: [],
  otherDownloadUrl: undefined,
  activityUrl: undefined,
  activityCode: undefined,
  testStatus: undefined,
  tips: undefined,
  home: undefined,
  status: undefined
})
const formRules = reactive({
  resourceClassifyName: [{ required: true, message: '资源分类名称不能为空', trigger: 'blur' }],
  name: [{ required: true, message: '资源名称不能为空', trigger: 'blur' }],
  picUrl: [{ required: true, message: '封面url不能为空', trigger: 'blur' }],
  supportSystemList: [{ required: true, message: '支持系统不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '上架状态不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ResourceDirApi.getResourceDir(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ResourceDirVO
    if (formType.value === 'create') {
      await ResourceDirApi.createResourceDir(data)
      message.success(t('common.createSuccess'))
    } else {
      await ResourceDirApi.updateResourceDir(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    resourceClassifyName: undefined,
    resourceClassifyId: undefined,
    name: undefined,
    alias: undefined,
    detail: undefined,
    remark: undefined,
    picUrl: undefined,
    videoUrl: undefined,
    supportSystemList: [],
    otherDownloadUrl: undefined,
    activityUrl: undefined,
    activityCode: undefined,
    testStatus: undefined,
    tips: undefined,
    home: undefined,
    status: undefined
  }
  formRef.value?.resetFields()
}

/** 初始化 **/
onMounted(() => {
  ResourceClassifyApi.getResourceClassifyList().then((data) => {
    console.log(data, 'ssss')
    classifyList.value = data
  })
})
</script>
